//
//  YDCarBrandCell.m
//  YDTestDrive
//
//  Created by ios on 2017/2/16.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDCarBrandCell.h"
#import "YDCarBrandModel.h"
#import "YDCarSeriesModel.h"

#import "UIImageView+WebCache.h"


@interface YDCarBrandCell ()

@property (nonatomic, strong) UIView *brandBGView;
@property (nonatomic, strong) UIImageView *brandImageView;
@property (nonatomic, strong) UILabel *brandNameLabel;
@property (nonatomic, strong) UIView *lineView;

@end
@implementation YDCarBrandCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = kViewControllerBackgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createView];
    }
    return self;
}

- (void)createView
{
    _brandBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44.0f)];
    _brandBGView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_brandBGView];
    
    _brandImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 7.0f, 35.0f, 30.0f)];
    _brandImageView.contentMode = UIViewContentModeScaleAspectFit;
    [_brandBGView addSubview:_brandImageView];
    
    _brandNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_brandImageView.maxX + 5.0f, 0, 200.0f, kCarBrandHeight)];
    _brandNameLabel.text = @"丰田";
    _brandNameLabel.font = [UIFont systemFontOfSize:14.0f];
    _brandNameLabel.textColor = [UIColor colorWithHexString:@"354052"];
    [_brandBGView addSubview:_brandNameLabel];
    
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, kCarBrandHeight - 0.5f, kScreenWidth, 0.5f)];
    _lineView.backgroundColor = kTableViewLineColor;
    [_brandBGView addSubview:_lineView];
    
    UIView *carSeriesView = [self crateCarSeriesViewWithModel:nil buttonTag:0];
    carSeriesView.frame = CGRectMake(0, _brandBGView.maxY + (kCarSeriesHeight * 0), kScreenWidth, kCarSeriesHeight);
    [self.contentView addSubview:carSeriesView];
}

- (void)setCarBrandModel:(YDCarBrandModel *)carBrandModel
{
    _carBrandModel = carBrandModel;
    
    _brandNameLabel.text = carBrandModel.brandName;
    
    [_brandImageView sd_setImageWithURL:[NSURL URLWithString:carBrandModel.logoPath] placeholderImage:[UIImage imageNamed:@"default_car"]];
    _brandImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    for (UIView *view in self.contentView.subviews) {
        if (view.tag == 1013) {
            [view removeFromSuperview];
        }
    }
    
    for (int i = 0; i < carBrandModel.carCount; i++) {
        YDCarSeriesModel *carSeriesModel = carBrandModel.carSeriesArray[i];
        UIView *carSeriesView = [self crateCarSeriesViewWithModel:carSeriesModel buttonTag:i];
        carSeriesView.frame = CGRectMake(0, _brandBGView.maxY + (kCarSeriesHeight * i), kScreenWidth, kCarSeriesHeight);
        [self.contentView addSubview:carSeriesView];
    }
    
}

- (UIView *)crateCarSeriesViewWithModel:(YDCarSeriesModel *)model buttonTag:(NSInteger)buttonTag
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *carImageView = [[UIImageView alloc] initWithFrame:CGRectMake(21.0f, (kCarSeriesHeight - 31.0f) / 2, 44.0f, 31.0f)];
    [carImageView sd_setImageWithURL:[NSURL URLWithString:model.logoPath] placeholderImage:[UIImage imageNamed:@"default_car"]];
    carImageView.layer.borderColor = kTableViewLineColor.CGColor;
    carImageView.layer.borderWidth = 0.5f;
    [view addSubview:carImageView];
    
    UILabel *carLabel = [[UILabel alloc] init];
    carLabel.frame = CGRectMake(carImageView.maxX + 10.0f, 0, kScreenWidth - carImageView.maxX - 10.0f, kCarSeriesHeight);
    carLabel.text = model.carsName;
    carLabel.font = [UIFont systemFontOfSize:15.0f];
    carLabel.textColor = [UIColor colorWithHexString:@"354052"];
    [view addSubview:carLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, kCarSeriesHeight - 0.5f, carLabel.width, 0.5f)];
    line.backgroundColor = kTableViewLineColor;
    [carLabel addSubview:line];
    
    UIButton *carButton = [UIButton buttonWithType:UIButtonTypeCustom];
    carButton.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    carButton.tag = buttonTag;
    [carButton addTarget:self action:@selector(carButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:carButton];
    
    view.tag = 1013;
    return view;
}

- (void)carButtonClick:(UIButton *)sender
{
    YDCarSeriesModel *model = [self.carBrandModel.carSeriesArray objectAtIndex:sender.tag];
    if ([self.delegete respondsToSelector:@selector(carButtonClickWithCarBrandModel:carSeriesModel:)]) {
        [self.delegete carButtonClickWithCarBrandModel:self.carBrandModel carSeriesModel:model];
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
